﻿using System.IO;
using System.Net;
using System.Xml.Linq;

namespace WP7.Extensions.Extensions
{
    public static class WebResponseExtensions
    {
        public static XDocument ToXmlDocument(this WebResponse response)
        {
            XDocument xDoc = null;
            try
            {
                using (var stream = response.GetResponseStream())
                {
                    if (stream != null)
                    {
                        using (var streamReader = new StreamReader(stream))
                        {
                            xDoc = XDocument.Load(streamReader);
                            streamReader.Close();
                        }

                        stream.Close();
                    }
                }

                response.Close();

            }
            catch (WebException ex)
            {
                throw ex;
            }

            return xDoc;
        }
    }
}
